package su.samcom.calc.controller.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.Model;
import su.samcom.calc.controller.form.PricingForm;
import su.samcom.calc.model.entity.Location;

/**
 * Created by Владислав on 21.01.14.
 */
public class PricingFormValidator {

    public final static String LOCATION_FROM_ERROR = "locationFromError";
    public final static String LOCATION_TO_ERROR = "locationToError";
    public final static String COMMON_ERROR = "commonError";

    public static boolean validate(PricingForm pricingForm, Model model) {
        boolean valid = true;
        if(pricingForm.getCountryFromId() == null || StringUtils.isBlank(pricingForm.getLocationFrom()) ){
            model.addAttribute(LOCATION_FROM_ERROR,"Введите пункт отправки");
            valid = false;
        }
        if(pricingForm.getCountryToId() == null || StringUtils.isBlank(pricingForm.getLocationTo()) ){
            model.addAttribute(LOCATION_TO_ERROR,"Введите пункт назначения");
            valid = false;
        }
        return valid;
    }


    public static boolean validateLocation(Location locationFrom, Location locationTo, Model model) {
        boolean valid = true;
        if(locationFrom == null){
            model.addAttribute(LOCATION_FROM_ERROR, "Пункт отправки не найден");
            valid = false;
        }
        if(locationTo == null){
            model.addAttribute(LOCATION_TO_ERROR, "Пункт назначения не найден");
            valid = false;
        }
        return valid;
    }

}
